************************************************************
*TABLE 1
************************************************************
*HH Atributes

local var2 c1 c2_1_rp c2_2_rp c2_3_rp c3 c5

estpost tabstat `var2'  if female == 1 , statistics(mean sd min max) columns(statistics)

esttab using "$outdir/Table1_HH&Care.tex", ///
    cells("mean(fmt(3) label(Mean)) sd(fmt(3) label(Std.\ Dev.))  ") ///
	nostar nonumbers nomtitle label booktabs width(38em) replace
	
************************************************************
*TABLE 2
************************************************************	
*IND Atributes

local var4 f6 level_1 level_2 level_3 employed unemployed inactive e102 flexibility_1 flexibility_2 flexibility_3 e107 job_search_1 job_search_2 job_search_3 job_search_4

estpost tabstat `var4', statistics(mean sd min max n) columns(statistics)

esttab using "$outdir/Table2_IndividualCharacteristics_ALL.tex", ///
	cells("mean(fmt(3) label(Mean)) sd(fmt(3) label(Std.\ Dev.))  min(fmt(0) label(Min.))  max(fmt(0) label(Max.)) n(fmt(0) label(N))") ///
	nostar nonumbers nomtitle label booktabs width(38em) replace
	
iebaltab `var4', groupvar(female)  savetex("$outdir/Table2_IndividualCharacteristics_Gender.tex") replace rowvarlabels stats(f(f))
*/

************************************************************
*TABLE 3. Beliefs
************************************************************

local bel2 fob2 sob2 sob2m sob2p fob9
iebaltab `bel2' [fw=fexp2], groupvar(female)  savetex("$outdir/Table3_BaselineBeliefs.tex") replace rowvarlabels  stats(f(f))



* -----------------------------------------------------------------
* Labels for respondent sex (optional, safe)
* -----------------------------------------------------------------
capture label drop sex
label define sex 0 "Husbands" 1 "Wives"
label values female sex

* Use monochrome scheme (QJE/AER-friendly)
set scheme s2mono


* -----------------------------------------------------------------
* 1. Pluralistic ignorance about WOMEN's support (sob2)
*     gap_w = perceived women's support – actual women's support
* -----------------------------------------------------------------

* Replace 90.5 with the actual baseline % of women who support
scalar true_women_support = 90.5

capture drop gap_w
gen gap_w = sob2 - true_women_support

capture drop x_m_w d_m_w x_f_w d_f_w
kdensity gap_w if female==0 [aw=fexp2], gen(x_m_w d_m_w) n(80) bwidth(8)
kdensity gap_w if female==1 [aw=fexp2], gen(x_f_w d_f_w) n(80) bwidth(8)

twoway ///
    (line d_m_w x_m_w, lpattern(solid)) ///
    (line d_f_w x_f_w, lpattern(dash))  ///
    , legend(order(1 "Husbands" 2 "Wives") pos(6) ring(0) ///
             region(color(white))) ///
      xline(0, lpattern(dash)) ///
      xscale(range(-80 20)) ///
      xlabel(-80(20)20) ///
      ytitle("Kernel density") ///
      xtitle("Perception minus actual support (pp)") ///
      graphregion(color(white)) ///
      plotregion(color(white)) ///
      name(fig_sob2, replace)

graph export "$outdir/Fig_PIgnorance_sob2.png", ///
    name(fig_sob2) replace width(2000)


* -----------------------------------------------------------------
* 2. Pluralistic ignorance about MEN's support (sob2m)
*     gap_m = perceived men's support – actual men's support
* -----------------------------------------------------------------

* Replace 80.2 with the actual baseline % of men who support
scalar true_men_support = 80.2

capture drop gap_m
gen gap_m = sob2m - true_men_support

capture drop x_m_m d_m_m x_f_m d_f_m
kdensity gap_m if female==0 [aw=fexp2], gen(x_m_m d_m_m) n(80) bwidth(8)
kdensity gap_m if female==1 [aw=fexp2], gen(x_f_m d_f_m) n(80) bwidth(8)

twoway ///
    (line d_m_m x_m_m, lpattern(solid)) ///
    (line d_f_m x_f_m, lpattern(dash))  ///
    , legend(order(1 "Husbands" 2 "Wives") pos(6) ring(0) ///
             region(color(white))) ///
      xline(0, lpattern(dash)) ///
      xscale(range(-80 20)) ///
      xlabel(-80(20)20) ///
      ytitle("Kernel density") ///
      xtitle("Perception minus actual support (pp)") ///
      graphregion(color(white)) ///
      plotregion(color(white)) ///
      name(fig_sob2m, replace)

graph export "$outdir/Fig_PIgnorance_sob2m.png", ///
    name(fig_sob2m) replace width(2000)

************************************************************
*TABLE 5. Balance Test for Different Samples 
************************************************************
* Subsamples used in Tables 6–9


local samples sample0 sample1_ext sample2 sample3_ext sample5_ext 

local sample4 sample4_both sample4_any sample4_other sample4_none_ext

* Belief blocks (already defined earlier, I just gather them)
local bel1 fob1 sob1 sob1sdb sob1m 
local bel2 fob2 sob2 sob2m sob2p
local bel3 fob3 sob3 sob3sdb sob3m sob3p
local bel4 fob4 fob6
local bel5 fob5 sob5 sob5m sob5p
local bel6 fob7 sob7 sob7m sob7p
local bel7 fob8 sob8 sob8m sob8p
local bel8 e501 e502 e503 e501b e504 e505 e506 e504b
local bel9 fob9

* Collect all belief variables you want
local bels  `bel2'

* Baseline covariates (non-beliefs)
local xvars female c1 c2_2_rp c3 c5 f6 age_dif  level_1 level_2 edu_WmoreH inactiveM unemployedM estrato_12 estrato_3 tec_2 

local strat inactiveF employedF fob2M

************************************************************
* Selective exposure – all baseline predictors of D
************************************************************
eststo clear

local i = 0
foreach s of local samples {
    local ++i

    quietly reg D `xvars' `bels' `strat' if `s'==1, vce(cluster id)

    * Joint test: all baseline variables (xvars + beliefs)
    quietly testparm `xvars' `bels'
    estadd scalar F_all = r(F)
    estadd scalar p_all = r(p)

    * Joint test: beliefs only
    quietly testparm `bels'
    estadd scalar F_bel = r(F)
    estadd scalar p_bel = r(p)

    * Joint test: non-belief covariates only
    quietly testparm `xvars'
    estadd scalar F_x = r(F)
    estadd scalar p_x = r(p)

    eststo se`i'
}

estout se* using "$outdir/Table_5_Balance.tex", ///
    style(tex) label replace ///
    cells(b(star fmt(3)) se(par fmt(3))) ///
    stats(N r2 F_all p_all F_bel p_bel F_x p_x, ///
          fmt(0 3 2 3 2 3 2 3) ///
          labels("N" "R-sq" ///
                 "F (all baseline)" "p (all baseline)" ///
                 "F (beliefs)"      "p (beliefs)"     ///
                 "F (covariates)"   "p (covariates)")) ///
    substitute(_ _)

eststo clear

	/*
local i = 0
foreach s of local sample4 {
    local ++i

    quietly reg D `xvars' `bels' `strat' if `s'==1, vce(cluster id)

    * Joint test: all baseline variables (xvars + beliefs)
    quietly testparm `xvars' `bels'
    estadd scalar F_all = r(F)
    estadd scalar p_all = r(p)

    * Joint test: beliefs only
    quietly testparm `bels'
    estadd scalar F_bel = r(F)
    estadd scalar p_bel = r(p)

    * Joint test: non-belief covariates only
    quietly testparm `xvars'
    estadd scalar F_x = r(F)
    estadd scalar p_x = r(p)

    eststo se`i'
}

estout se* using "$outdir/Table_5_Balance_Appendix.tex", ///
    style(tex) label replace ///
    cells(b(star fmt(3)) se(par fmt(3))) ///
    stats(N r2 F_all p_all F_bel p_bel F_x p_x, ///
          fmt(0 3 2 3 2 3 2 3) ///
          labels("N" "R-sq" ///
                 "F (all baseline)" "p (all baseline)" ///
                 "F (beliefs)"      "p (beliefs)"     ///
                 "F (covariates)"   "p (covariates)")) ///
    substitute(_ _)
*/
	
************************************************************
* Selective Attrition – all baseline predictors of D
************************************************************
eststo clear
local samples sample1_ext sample2 sample3_ext sample5_ext 

local i = 0
foreach s of local samples {
    local ++i

    quietly reg `s' D  `xvars' `bels' `strat', vce(cluster id)

    * Joint test: all baseline variables (xvars + beliefs)
    quietly testparm `xvars' `bels'
    estadd scalar F_all = r(F)
    estadd scalar p_all = r(p)

    * Joint test: beliefs only
    quietly testparm `bels'
    estadd scalar F_bel = r(F)
    estadd scalar p_bel = r(p)

    * Joint test: non-belief covariates only
    quietly testparm `xvars'
    estadd scalar F_x = r(F)
    estadd scalar p_x = r(p)

    eststo se`i'
}

estout se* using "$outdir/Table_A3_Attrition.tex", ///
    style(tex) label replace ///
    cells(b(star fmt(3)) se(par fmt(3))) ///
    stats(N r2 F_all p_all F_bel p_bel F_x p_x, ///
          fmt(0 3 2 3 2 3 2 3) ///
          labels("N" "R-sq" ///
                 "F (all baseline)" "p (all baseline)" ///
                 "F (beliefs)"      "p (beliefs)"     ///
                 "F (covariates)"   "p (covariates)")) ///
    substitute(_ _)

eststo clear

/*
	
local i = 0
foreach s of local sample4 {
    local ++i

    quietly reg `s' D sample1_ext `xvars' `bels' `strat', vce(cluster id)

    * Joint test: all baseline variables (xvars + beliefs)
    quietly testparm `xvars' `bels'
    estadd scalar F_all = r(F)
    estadd scalar p_all = r(p)

    * Joint test: beliefs only
    quietly testparm `bels'
    estadd scalar F_bel = r(F) 
    estadd scalar p_bel = r(p)

    * Joint test: non-belief covariates only
    quietly testparm `xvars'
    estadd scalar F_x = r(F)
    estadd scalar p_x = r(p)

    eststo se`i'
}

estout se* using "$outdir/Table_A3_AttritionS4.tex", ///
    style(tex) label replace ///
    cells(b(star fmt(3)) se(par fmt(3))) ///
    stats(N r2 F_all p_all F_bel p_bel F_x p_x, ///
          fmt(0 3 2 3 2 3 2 3) ///
          labels("N" "R-sq" ///
                 "F (all baseline)" "p (all baseline)" ///
                 "F (beliefs)"      "p (beliefs)"     ///
                 "F (covariates)"   "p (covariates)")) ///
    substitute(_ _)
*/
